#include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>

void menu(SDL_Surface *ecran);

int main(int argc, char *argv[])
{
    SDL_Surface *ecran;
    SDL_Init(SDL_INIT_VIDEO);

    ecran=SDL_SetVideoMode(800, 400, 32, SDL_HWSURFACE);

    SDL_WM_SetCaption("SUDOKU ESPRIT 2P4", NULL);
    SDL_WM_SetIcon(SDL_LoadBMP("sdk.bmp"), NULL);
    SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 255));
    SDL_Flip(ecran);



    menu(ecran);


    SDL_Quit();

    return EXIT_SUCCESS;
    SDL_FreeSurface(ecran);
}

void menu(SDL_Surface *ecran)

{
    SDL_Rect position_menu,posp;
    SDL_Event event;
    int m,a,n,j,d,f,l,k,ll,kk,o,s,v;
    /*int t[9][9];
    int s1[9][9]={{3,2,9,6,7,1,4,5,8},
                 {7,4,1,5,2,8,3,6,9},
                 {6,5,8,4,3,9,2,7,1},
                 {8,6,2,9,1,5,7,4,3},
                 {9,3,4,2,8,7,5,1,6},
                 {1,7,5,3,6,4,9,8,2},
                 {5,9,6,1,4,3,8,2,7},
                 {2,8,3,7,5,6,1,9,4},
                 {4,1,7,8,9,2,6,3,5}};*/
    int continuer = 1;
    SDL_Surface* image_bmp = SDL_LoadBMP("om1.bmp");
    SDL_Surface* menu_bmp = SDL_LoadBMP("menu5.bmp");
    SDL_Surface* menu0_bmp = SDL_LoadBMP("menu1.bmp");
    SDL_Surface* menu1_bmp = SDL_LoadBMP("menu2.bmp");
    SDL_Surface* menu2_bmp = SDL_LoadBMP("menu3.bmp");
    SDL_Surface* menu3_bmp = SDL_LoadBMP("menu4.bmp");
    SDL_Surface* aide_bmp = SDL_LoadBMP("aide1.bmp");
    SDL_Surface* aide1_bmp = SDL_LoadBMP("aide2.bmp");
    SDL_Surface* niveau1_bmp = SDL_LoadBMP("niv1.bmp");
    SDL_Surface* niveau2_bmp = SDL_LoadBMP("niv2.bmp");
    SDL_Surface* niveau3_bmp = SDL_LoadBMP("niv3.bmp");
    SDL_Surface* jeu1_bmp = SDL_LoadBMP("grille1..bmp");
    SDL_Surface* jeu2_bmp = SDL_LoadBMP("grille2.bmp");
    SDL_Surface* jeu3_bmp = SDL_LoadBMP("grille3.bmp");
    SDL_Surface*num_bmp = SDL_LoadBMP("om1.bmp");

    SDL_Surface* image1_bmp = SDL_LoadBMP("im1.bmp");
    SDL_Surface* image2_bmp = SDL_LoadBMP("im2.bmp");
    SDL_Surface* image3_bmp = SDL_LoadBMP("im3.bmp");
    SDL_Surface* image4_bmp = SDL_LoadBMP("im4.bmp");
    SDL_Surface* image5_bmp = SDL_LoadBMP("im5.bmp");
    SDL_Surface* image6_bmp = SDL_LoadBMP("im6.bmp");
    SDL_Surface* image7_bmp = SDL_LoadBMP("im7.bmp");
    SDL_Surface* image8_bmp = SDL_LoadBMP("im8.bmp");
    SDL_Surface* image9_bmp = SDL_LoadBMP("im9.bmp");
    SDL_Surface* bravo1_bmp = SDL_LoadBMP("bravo.bmp");
    SDL_Surface* option1_bmp = SDL_LoadBMP("on.bmp");
    SDL_Surface* option2_bmp = SDL_LoadBMP("off.bmp");
    SDL_Surface* score_bmp = SDL_LoadBMP("score.bmp");
    SDL_Surface* solution_bmp = SDL_LoadBMP("solution11.bmp");
    posp.x =0;
    posp.y =0;
    /*t[1][3]=9;
    t[1][4]=6;
    t[1][6]=1;
    t[2][1]=7;
    t[2][3]=1;
    t[2][4]=5;
    t[2][6]=8;
    t[2][8]=6;
    t[3][1]=6;
    t[3][3]=8;
    t[3][4]=4;
    t[3][7]=2;
    t[3][8]=7;
    t[3][9]=1;
    t[4][3]=2;
    t[4][5]=1;
    t[4][6]=5;
    t[4][8]=4;
    t[4][9]=3;
    t[5][1]=9;
    t[5][2]=3;
    t[5][4]=2;
    t[5][5]=8;
    t[5][9]=6;
    t[6][1]=1;
    t[6][2]=7;
    t[6][3]=5;
    t[6][6]=4;
    t[6][9]=2;
    t[7][6]=3;
    t[7][7]=8;
    t[7][9]=7;
    t[8][3]=3;
    t[8][8]=9;
    t[8][9]=4;
    t[9][5]=9;
    t[9][6]=2;
    t[9][9]=5;

    k=0;
    l=0;*/
    position_menu.x =0;
    position_menu.y =0;

    SDL_BlitSurface(menu_bmp, NULL, ecran, &position_menu);
    SDL_Flip(ecran);


    m=0;
    n=0;
    a=0;
    j=0;
    v=0;
    while (continuer)
    {
        f=0;

        //menu logic
        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                continuer = 0;
            case SDL_KEYDOWN:
            {
                if (event.key.keysym.sym == SDLK_ESCAPE && m==0 && a==0 &&s==0&&n==0)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_ESCAPE && (o!=0 || a!=0 || s!=0 || n!=0 || j!=0||v!=0 ))
                {

                    a=0;
                    o=0;
                    s=0;
                    n=0;
                    j=0;
                    v=0;
                    d=0;
                    f=0;

                }
                if (event.key.keysym.sym == SDLK_BACKSPACE)
                    continuer=0;
                if (event.key.keysym.sym == SDLK_RETURN && m==4)
                    continuer=0;

                if (event.key.keysym.sym == SDLK_RETURN && m==3)

                    a++;
                if (event.key.keysym.sym == SDLK_RIGHT && m==3 && a==1)
                    a++;
                if (event.key.keysym.sym == SDLK_LEFT  && m==3 && a==2)
                    a--;

                if (event.key.keysym.sym == SDLK_RIGHT&& a==0 && o==0 && s==0  && n==0)
                    m++;
                if (event.key.keysym.sym == SDLK_LEFT && a==0 && o==0 && s==0 && n==0)
                    m--;
                if (event.key.keysym.sym == SDLK_RETURN && m==2 && o==0 )

                    o++;
                if (event.key.keysym.sym == SDLK_RIGHT && m==2 && o==1)
                    o++;
                if (event.key.keysym.sym == SDLK_LEFT && m==2 && o==2)
                    o--;
                if (event.key.keysym.sym == SDLK_RETURN && m==1 && s==0)
                    s++;
                if (event.key.keysym.sym == SDLK_ESCAPE && m==1&& s==1)
                    s--;

                if (event.key.keysym.sym == SDLK_RETURN && m==0 && n==0)
                    n++;
                if (event.key.keysym.sym == SDLK_RIGHT && m==0 && n!=0&&j==0)
                {
                    if(n==3)
                        n=n+2;
                    else
                        n++;
                }
                if (event.key.keysym.sym == SDLK_LEFT&& m==0 && n!=0&&j==0)
                    if(n==1)
                        n=n+2;
                    else n--;
                if (event.key.keysym.sym == SDLK_p && m==0 && n==1 && j==0)
                    j++;
                if (event.key.keysym.sym == SDLK_v&& m==0 && n==1&&j==1&&v==0)
                    v++;
                if (event.key.keysym.sym == SDLK_r&& m==0 && n==1&&j==1&&v==1)
                    v--;
                do
                {
                    if (event.key.keysym.sym == SDLK_RIGHT && m==0 && n==1&&j==1)
                    {
                        posp.x=posp.x+44;
                        d=1;
                    }
                    //l++;}
                    if (event.key.keysym.sym == SDLK_LEFT && m==0 && n==1&&j==1)
                    {
                        posp.x=posp.x-44;
                        d=2;
                        //l--;
                    }
                    if (event.key.keysym.sym == SDLK_DOWN && m==0 && n==1&&j==1)
                    {
                        posp.y=posp.y+44;
                        d=3;
                        //k++;
                    }
                    if (event.key.keysym.sym == SDLK_UP && m==0 && n==1&&j==1)
                    {
                        posp.y=posp.y-44;
                        d=4;
                        //k--;
                    }
                    if (event.key.keysym.sym == SDLK_1 && m==0 && n==1&&j==1)
                        f=1;
                    //t[l][k]=1;}

                    if (event.key.keysym.sym == SDLK_2 && m==0 && n==1&&j==1)
                        f=2;
                    // t[l][k]=2;}
                    if (event.key.keysym.sym == SDLK_3 && m==0 && n==1&&j==1)
                        f=3;
                    //t[l][k]=3;}
                    if (event.key.keysym.sym == SDLK_4 && m==0 && n==1&&j==1)
                        f=4;
                    //t[l][k]=4;}
                    if (event.key.keysym.sym == SDLK_5 && m==0 && n==1&&j==1)
                        f=5;
                    //t[l][k]=5;}
                    if (event.key.keysym.sym == SDLK_6 && m==0 && n==1&&j==1)
                        f=6;
                    //t[l][k]=6;}
                    if (event.key.keysym.sym == SDLK_7 && m==0 && n==1&&j==1)
                        f=7;
                    //t[l][k]=7;}
                    if (event.key.keysym.sym == SDLK_8 && m==0 && n==1&&j==1)
                        f=8;
                    //t[l][k]=8;}
                    if (event.key.keysym.sym == SDLK_9&& m==0 && n==1&&j==1)
                        f=9;
                    //t[l][k]=9;}
                }

                while(posp.x>393 || posp.y>393 );






                break;
            }

            }




        }

        //partie 2 : affichage des entites

        SDL_BlitSurface(menu_bmp, NULL, ecran, &position_menu);
        m=m%5;
        a=a%3;
        o=o%3;
        s=s%2;
        n=n%4;

        if(m == 0 )
        {
            SDL_BlitSurface(menu0_bmp, NULL, ecran, &position_menu);

            if(n==1)
            {
                SDL_BlitSurface(niveau1_bmp, NULL, ecran, &position_menu);
                if( j==1)
                {

                    SDL_BlitSurface(jeu1_bmp, NULL, ecran, &position_menu);
                    SDL_BlitSurface(image_bmp, NULL, ecran, &posp);
                    if(d==1)
                        SDL_BlitSurface(num_bmp, NULL, ecran, &posp);
                    if(d==2)
                        SDL_BlitSurface(num_bmp, NULL, ecran, &posp);
                    if(d==3)
                        SDL_BlitSurface(num_bmp, NULL, ecran, &posp);
                    if(d==4)
                        SDL_BlitSurface(num_bmp, NULL, ecran, &posp);


                }
                if(v==1)
                {
                    SDL_BlitSurface(solution_bmp, NULL, ecran, &position_menu);

                }
                if(f==1)
                {
                    SDL_BlitSurface(image1_bmp, NULL,jeu1_bmp, &posp);
                    SDL_BlitSurface(image1_bmp, NULL,ecran, &posp);
                }

                if(f==2)
                {
                    SDL_BlitSurface(image2_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image2_bmp, NULL, ecran, &posp);
                }
                if(f==3)
                {
                    SDL_BlitSurface(image3_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image3_bmp, NULL,  ecran, &posp);
                }
                if(f==4)
                {
                    SDL_BlitSurface(image4_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image4_bmp, NULL,  ecran, &posp);
                }
                if(f==5)
                {
                    SDL_BlitSurface(image5_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image5_bmp, NULL,  ecran, &posp);
                }
                if(f==6)
                {
                    SDL_BlitSurface(image6_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image6_bmp, NULL,  ecran, &posp);
                }
                if(f==7)
                {
                    SDL_BlitSurface(image7_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image7_bmp, NULL,  ecran, &posp);
                }
                if(f==8)
                {
                    SDL_BlitSurface(image8_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image8_bmp, NULL,  ecran, &posp);
                }
                if(f==9)
                {
                    SDL_BlitSurface(image9_bmp, NULL, jeu1_bmp, &posp);
                    SDL_BlitSurface(image9_bmp, NULL,  ecran, &posp);
                }
            }
            if(n==2)
                SDL_BlitSurface(niveau2_bmp, NULL, ecran, &position_menu);
            if(n==3)
                SDL_BlitSurface(niveau3_bmp, NULL, ecran, &position_menu);
        }

        if(m == 1)
        {
            SDL_BlitSurface(menu1_bmp, NULL, ecran, &position_menu);
            if(s==1)
                SDL_BlitSurface(score_bmp, NULL, ecran, &position_menu);
        }


        if(m == 2)
        {
            SDL_BlitSurface(menu2_bmp, NULL, ecran, &position_menu);

            if(o==1)
                SDL_BlitSurface(option1_bmp, NULL, ecran, &position_menu);
            if(o==2)
                SDL_BlitSurface(option2_bmp, NULL, ecran, &position_menu);
        }
        if(m == 3)
        {
            SDL_BlitSurface(menu3_bmp, NULL, ecran, &position_menu);
            if(a==1)

                SDL_BlitSurface(aide_bmp, NULL, ecran, &position_menu);
            if(a==2)
                SDL_BlitSurface(aide1_bmp, NULL, ecran, &position_menu);
        }

        //if(m==0 && n==1&& j==1)
        //SDL_BlitSurface(jeu1_bmp, NULL, ecran, &position_menu);
        /*ll=0;

        while(t[ll][kk]==s1[ll][kk]&&ll<9)
        {kk=0;
            while(t[ll][kk]==s1[ll][kk]&&kk<9)
            kk++;
        ll++;
        }
        if(ll==9)
           SDL_BlitSurface(bravo1_bmp, NULL, jeu1_bmp, &position_menu);*/


        SDL_Flip(ecran); //action  3 et 4 dans game loop

    }
}




